# -*- coding: utf-8 -*-
import datetime

from sqlalchemy import Column, String, SmallInteger, BigInteger, Text
from com.maycur.utils.MySqlUtils import Base, BaseOperationEntity, get_session


class RoleInformation(Base, BaseOperationEntity):
    __tablename__ = "role_information"

    id = Column(BigInteger, primary_key=True)
    ent_code = Column(String, nullable=False)
    role_code = Column(String, nullable=True)
    role_name = Column(String, nullable=True)
    role_biz_code = Column(String, nullable=True)
    source_role_code = Column(String, nullable=True)
    comments = Column(Text, nullable=True)
    is_deleted = Column(SmallInteger, nullable=False, default=0)
    customize_level = Column(SmallInteger, nullable=False, default=1)


class RoleMapper:
    @staticmethod
    def get_by_role_code(ent_code: str, role_code: str) -> RoleInformation:
        with get_session() as s:
            return s.query(RoleInformation) \
                .filter(RoleInformation.ent_code == ent_code) \
                .filter(RoleInformation.role_code == role_code)\
                .filter(RoleInformation.is_deleted == 0).one()

    @staticmethod
    def create_role(the_role: RoleInformation):
        with get_session() as s:
            s.add(the_role)
            s.commit()

    @staticmethod
    def update_role_name(the_role: RoleInformation):
        with get_session() as s:
            update_param = {
                'role_name': the_role.role_name,
                'updated_at': datetime.datetime.now(),
                'updated_by': 'DBA'
            }
            s.query(RoleInformation).filter(RoleInformation.ent_code == the_role.ent_code,
                                            RoleInformation.role_code == the_role.role_code).update(update_param)

    @staticmethod
    def delete_role_by_role_code(role_code: str, soft_delete: bool):
        with get_session() as s:
            if soft_delete:
                update_param = {
                    'is_deleted': 1,
                    'updated_at': datetime.datetime.now(),
                    'updated_by': 'DBA'
                }
                s.query(RoleInformation).filter(RoleInformation.role_code == role_code).update(update_param)
            else:
                # 硬删除
                s.query(RoleInformation).filter(RoleInformation.role_code == role_code).delete()


class DatabaseOperationDemo:

    role_mapper: RoleMapper

    def __init__(self):
        self.role_mapper = RoleMapper()

    def create_role(self):
        role = RoleInformation()
        role.ent_code = 'EntCode883366252'
        role.role_name = '测试角色名称'
        role.role_code = 'TestRoleCode883366252'
        role.created_by = 'DBA'
        role.comments = '一些注释'

        self.role_mapper.create_role(role)

    def update_role(self):
        role = RoleInformation()
        role.ent_code = 'EntCode883366252'
        role.role_name = '测试角色名称-修改'
        role.role_code = 'TestRoleCode883366252'
        self.role_mapper.update_role_name(role)

    def delete_role(self):
        self.role_mapper.delete_role_by_role_code('TestRoleCode883366252', False)

    def get_role(self):

        res = self.role_mapper.get_by_role_code('EntCode883366252', 'TestRoleCode883366252')
        if res is not None:
            print(f'role name is {res.role_name}')
        else:
            'role is deleted'


if __name__ == '__main__':
    db_demo = DatabaseOperationDemo()
    db_demo.create_role()
    db_demo.update_role()
    db_demo.get_role()
    db_demo.delete_role()
